{% extends "base.html" %}

{% block title %}创建会议室{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2>创建新会议室</h2>
            <button id="backBtn" class="btn btn-secondary">返回列表</button>
        </div>
        <div class="card-body">
            <form id="roomForm">
                <div class="mb-3">
                    <label for="name" class="form-label">会议室名称</label>
                    <input type="text" class="form-control" id="name" required>
                </div>
                <div class="mb-3">
                    <label for="capacity" class="form-label">容量（人数）</label>
                    <input type="number" class="form-control" id="capacity" min="1" value="10" required>
                </div>
                <div class="mb-3">
                    <label for="location" class="form-label">位置</label>
                    <input type="text" class="form-control" id="location" required>
                </div>
                <div class="mb-3">
                    <label for="facilities" class="form-label">设施</label>
                    <textarea class="form-control" id="facilities" rows="3" placeholder="例如：投影仪、白板、视频会议设备..."></textarea>
                </div>
                <button type="submit" class="btn btn-primary">创建会议室</button>
            </form>
        </div>
    </div>
</div>

<script>
    // 创建会议室
    async function createRoom(roomData) {
        try {
            const response = await axios.post('/api/rooms/', roomData);
            if (response.status === 201) {
                alert('会议室创建成功');
                window.location.href = '/rooms';
            }
        } catch (error) {
            console.error('Error creating room:', error);
            alert('创建会议室失败: ' + (error.response?.data?.detail || error.message));
        }
    }

    // 页面加载完成后执行
    document.addEventListener('DOMContentLoaded', function() {
        // 设置token
        const token = localStorage.getItem('token');
        if (token) {
            axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
        }
        
        // 返回按钮事件
        document.getElementById('backBtn').addEventListener('click', () => {
            window.location.href = '/rooms';
        });
        
        // 表单提交事件
        document.getElementById('roomForm').addEventListener('submit', (e) => {
            e.preventDefault();
            
            const roomData = {
                name: document.getElementById('name').value,
                capacity: parseInt(document.getElementById('capacity').value),
                location: document.getElementById('location').value,
                facilities: document.getElementById('facilities').value
            };
            
            createRoom(roomData);
        });
    });
</script>
{% endblock %} 